package com.alex.xmlyfm.tasks;

import com.alex.mylibrary.tasks.BaseTask;
import com.alex.mylibrary.tasks.TaskCallBack;
import com.alex.mylibrary.tasks.TaskResult;
import com.alex.xmlyfm.client.ClientAPI;
import com.alex.xmlyfm.entities.EntityParseUtil;
import com.alex.xmlyfm.entities.albumdetail.AlbumDetailHeader;
import com.alex.xmlyfm.entities.albumdetail.AlbumDetailTrack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-23.
 */
public class AlbumDetailTask extends BaseTask {
    public AlbumDetailTask(TaskCallBack callBack) {
        super(callBack);
    }

    @Override
    protected TaskResult doInBackground(String... params) {
        TaskResult result = null;
        long tractId = Long.parseLong(params[0]);

        JSONObject object = ClientAPI.getAlbumDetail(tractId);
        if (object != null) {
            try {
                result = new TaskResult();
                result.resultCode = object.getInt("ret");
                AlbumDetailHeader albumDetailHeader = EntityParseUtil.parseAlbumDetailHeader(object);
                List<AlbumDetailTrack> tracks = new LinkedList<>();
                tracks = EntityParseUtil.parseAlbumDetailTrack(object);
                result.datas = new Object[2];
                result.datas[0] = albumDetailHeader;
                result.datas[1] = tracks;

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}
